package com.tsingteng.cosfun.ui.message.chat;

import android.util.Log;

import com.tsingteng.cosfun.bean.ChatBean;
import com.tsingteng.cosfun.bean.PullBlackBean;
import com.tsingteng.cosfun.bean.UploadReportBean;
import com.tsingteng.cosfun.callback.RxObserver;
import com.tsingteng.cosfun.mvp.presenter.BasePresenter;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 私信页面P层
 * 常胜
 * 2018.8.16
 * */
public class ChatPresenter extends BasePresenter<ChatContract.IChatView> implements ChatContract.IChatPresenter {
    private ChatContract.IChatView view;
    private final ChatModel chatModel;

    public ChatPresenter(){
        chatModel = new ChatModel();
    }
    @Override
    public void loginChatData(String type, int page, int source) {
        view =  getView();
        RxObserver<ChatBean> rxObserver = new RxObserver<ChatBean>(this) {
            @Override
            protected void onFaile(int errorCode, String errorMsg) {

            }

            @Override
            protected void onSuccess(ChatBean listBean) {
                view.showChatData(listBean);
            }
        };
        chatModel.getOfficalNotification(type,page,source,rxObserver);
        addDisposable(rxObserver);
    }

    @Override
    public void loginPullBlack(String user_id) {
        view = getView();
        RxObserver<UploadReportBean> rxObserver = new RxObserver<UploadReportBean>(this) {
            @Override
            protected void onFaile(int errorCode, String errorMsg) {
                Log.e("-----",errorMsg);
            }

            @Override
            protected void onSuccess(UploadReportBean uploadReportBean) {
                view.showPullBlackResult(uploadReportBean);
            }
        };
        chatModel.getPullBlackData(user_id,rxObserver);
        addDisposable(rxObserver);
    }

    @Override
    public void loginSendMessage(String mContent, long sid) {
        view = getView();
        RxObserver<Integer> rxObserver = new RxObserver<Integer>(this) {
            @Override
            protected void onFaile(int errorCode, String errorMsg) {
             view.showFailResult(errorCode,errorMsg);
            }

            @Override
            protected void onSuccess(Integer integer) {
                view.sendMessageResult(integer);
            }
        };
        chatModel.getSendMessage(mContent,sid,rxObserver);
        addDisposable(rxObserver);
    }

    @Override
    public void loginChatRead(String type, int maxid, int sourceId) {
        view = getView();
        RxObserver<Integer> rxObserver = new RxObserver<Integer>(this) {
            @Override
            protected void onFaile(int errorCode, String errorMsg) {
                Log.e("^^^^^",errorMsg);
            }

            @Override
            protected void onSuccess(Integer integer) {
                view.showReadResult(integer);
            }
        };
        chatModel.getChatRead(type,maxid,sourceId,rxObserver);
        addDisposable(rxObserver);
    }
    @Override
    public void getReadPullBlackState(int user_id) {
        view = getView();
        Callback<PullBlackBean> workCallback = new Callback<PullBlackBean>() {
            @Override
            public void onResponse(Call<PullBlackBean> call, Response<PullBlackBean> response) {
                if (response != null) {
                    PullBlackBean body = response.body();
                    if (body != null) {
                        int data = body.getData();
                        view.showReadPullBlackStateResult(data);
                    }
                }

            }

            @Override
            public void onFailure(Call<PullBlackBean> call, Throwable t) {
            }
        };
        chatModel.getReadPullBlackStateInfo(user_id, workCallback);
    }
    @Override
    public void getCancelPullBlack(long user_id) {
        view = getView();
        Callback<PullBlackBean> workCallback = new Callback<PullBlackBean>() {
            @Override
            public void onResponse(Call<PullBlackBean> call, Response<PullBlackBean> response) {
                PullBlackBean body = response.body();
                int data = body.getData();
                view.showCancelPullBlack(data);
            }

            @Override
            public void onFailure(Call<PullBlackBean> call, Throwable t) {
                Log.e("/////", t.getMessage());
            }
        };
        chatModel.getCancelPullBlackInfo(user_id, workCallback);
    }
}
